<?php
/**
 * Project: AIGC
 * Author: moufer <moufer@163.com>
 * Date: 2024/7/6 12:28
 */

namespace module\ai\library\channel\abstract;

use module\ai\library\channel\Factory;
use module\ai\library\channel\interface\ConfigInterface;
use module\ai\library\channel\interface\KeyInterface;
use module\ai\model\Channel;
use mof\Model;
use mof\utils\DictArray;
use mof\utils\ElementData;
use mof\Validate;

abstract class Platform implements ConfigInterface, KeyInterface
{
    protected array $validate = [
        'rule'    => [
            'model|模型' => 'require'
        ],
        'message' => [],
    ];

    protected array $message = [];

    protected string $regUrl = '';

    protected bool $useProject = false;

    public static function models(): array
    {
        return [];
    }

    public function getConfigForm(Channel|Model $model = null): array
    {
        return [
            [
                "label"   => "语言模型",
                "prop"    => "config.model",
                "value"   => $model['config']['model'] ?? "",
                "type"    => "select",
                "options" => DictArray::make(static::models())->toElementData()->toSelectOptions(),
                "intro"   => "选择大语言模型类别。模型区别请前往模型所属平台查看。",
            ]
        ];
    }

    /**
     * 配置参数验证
     * @param array $data
     * @return void
     */
    public function validateConfig(array $data): void
    {
        $validate = new Validate();
        $validate->rule($this->validate['rule'])
            ->message($this->validate['message'] ?? [])
            ->failException()->check($data);
    }

    public function getKeysForm(array $config = null): array
    {
        $columns = $this->getKeyColumns();
        return array_map(fn($key, $item) => [
            "label" => $item['label'],
            "prop"  => "config.{$key}",
            "value" => $config[$key] ?? '',
            "intro" => $item['intro'] ?? '',
        ], array_keys($columns), $columns);
    }

    public function formattingKeyTitle(array $config): string
    {
        $result = [];
        foreach ($this->getKeyColumns() as $key => $data) {
            $value = empty($config[$key]) ? '' : $config[$key];
            if (!empty($data['table_hide'])) continue;
            if (!empty($data['secret'])) {
                //隐藏 $value 中间字符串为***
                $len = strlen($value);
                if ($len > 10) {
                    $value = substr($value, 0, 5) . str_repeat('*', 5) . substr($value, -5);
                } else if ($len > 5) {
                    $value = substr($value, 0, 5) . str_repeat('*', 4);
                } else {
                    $value = str_repeat('*', $len);
                }
            }
            $result[] = $value;
        }
        return implode('/', $result);
    }

    public function getKeyColumns(): array
    {
        return [
            'api_key' => [
                'label'  => 'API Key',
                'secret' => true,
            ],
        ];
    }

    public function getRegUrl(): string
    {
        return $this->regUrl;
    }

    public function getUseProject(): bool
    {
        return $this->useProject;
    }
}